package com.wgchao.identityrightsmanagement.application.service;

import com.wgchao.identityrightsmanagement.domain.organization.model.*;
import com.wgchao.identityrightsmanagement.domain.organization.repository.AgencyRepository;
import com.wgchao.identityrightsmanagement.domain.organization.repository.OfficeRepository;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import org.apache.catalina.core.ApplicationPushBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName: OfficeApplicationService
 * @author: wgchao
 * @createTime: 2020/6/8 2:33 PM
 * @Description: TODO
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OfficeApplicationService {
    @Autowired
    private AgencyRepository agencyRepository;
    @Autowired
    private OfficeRepository officeRepository;
    @Autowired
    private ApplicationEventPublisher pushBuilder;

    // 添加新职位
    public void addOffice(String agencyCode, OfficeCreator officeCreator, String operator){
        Agency agency = agencyRepository.findById(agencyCode);
        if(agency == null){
            throw new ObjectNotExistException("机构不存在");
        }
        String officeId = officeRepository.generateCode();
        Office office = Office.initOffice(agency, officeId, officeCreator);
        officeRepository.add(office, operator);
    }

    /**
     * 修改职位基本信息
     * @param code
     * @param officeUpdator
     * @param operator
     */
    public void modifyOfficeInfo(String code, OfficeUpdator officeUpdator, String operator){
        Office office = officeRepository.findById(code);
        if(office == null){
            throw new ObjectNotExistException("职位不存在");
        }
        Agency agency = agencyRepository.findById(office.getAgencyCode());
        if(agency == null){
            throw new ObjectNotExistException("机构不存在");
        }
        office.modifyOfficeInfo(officeUpdator, agency);
        officeRepository.add(office, operator);
    }

    /**
     * 停用职位
     * @param code
     * @param operator
     */
    public void disable(String code, String operator){
        Office office = officeRepository.findById(code);
        if(office == null){
            throw new ObjectNotExistException("职位不存在");
        }
        office.disable();
        officeRepository.add(office, operator);
    }

    /**
     * 启用职位
     * @param code
     * @param operator
     */
    public void regain(String code, String operator){
        Office office = officeRepository.findById(code);
        if(office == null){
            throw new ObjectNotExistException("职位不存在");
        }
        office.regain();
        officeRepository.add(office, operator);
    }

    public void remove(String code, String operator){
        Office office = officeRepository.findById(code);
        if(office == null){
            throw new ObjectNotExistException("职位不存在");
        }
        officeRepository.remove(office, operator);

        //发布事件 -清除部门职位关系 清除账户部门职位关系 清除部门职位权限
        pushBuilder.publishEvent(new OfficeRemovedEvent(new Date(), code, operator));
    }
}
